package com.huike.web.controller.system;

import com.huike.clues.domain.SysDictData;
import com.huike.clues.domain.SysDictType;
import com.huike.clues.domain.dto.my.DictExportDTO;
import com.huike.clues.domain.dto.my.DictQueryDTO;
import com.huike.clues.service.SysDictDataService;
import com.huike.common.annotation.Log;
import com.huike.common.annotation.RepeatSubmit;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysDictDataDTO;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.enums.BusinessType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 字典数据控制层
 */
@Slf4j
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController {

    @Autowired
    private SysDictDataService dictDataService;


    /**
     * 新增字典数据
     *
     * @param sysDictDataDTO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "字典管理", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping
    public AjaxResult addDictData(@Valid @RequestBody SysDictDataDTO sysDictDataDTO) {
        log.info("新增字典数据:{}", sysDictDataDTO);
        return toAjax(dictDataService.addDictData(sysDictDataDTO));
    }


    /**
     * 修改字典数据
     */
    @PutMapping
    public AjaxResult updateDictType(@RequestBody SysDictDataDTO sysDictDataDTO) {
        log.info("修改字典数据:{}", sysDictDataDTO);
        return toAjax(dictDataService.updateDictData(sysDictDataDTO));
    }



    /**
     * 分页查询字典数据信息
     */
    @GetMapping("/list")
    public TableDataInfo list(DictQueryDTO dictQueryDTO) {
        startPage();
        List<SysDictData> list = dictDataService.selectSysDictDataList(dictQueryDTO);
        return getDataTable(list);
    }

    /**
     * 导出字典数据
     *
     * @param dictExportDTO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @Log(title = "字典管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DictExportDTO dictExportDTO) {
        List<SysDictData> sysDictDataList = dictDataService.export(dictExportDTO);
        return AjaxResult.success(sysDictDataList);
    }

    /**


    /**
     * 根据字典类型查询字典数据信息
     *
     * @return
     */
    @GetMapping("/type/{dictType}")
    public AjaxResult selectDictType(@PathVariable String dictType) {
        List<SysDictData> sysDictDataList = dictDataService.selectByDictType(dictType);
        return AjaxResult.success(sysDictDataList);
    }

    /**
     * 删除字典数据
     *
     * @param dictCodes
     * @return
     */
    @DeleteMapping("/{dictCodes}")
    public AjaxResult delectDictData(@PathVariable Long[] dictCodes) {
        return toAjax(dictDataService.delectDictData(dictCodes));
    }

    /**
     * 查询字典数据详细信息
     *
     * @return
     */
    @GetMapping("/{dictCode}")
    public AjaxResult selectDictType(@PathVariable Long dictCode) {
        SysDictData sysDictData = dictDataService.selectDictData(dictCode);
        return AjaxResult.success(sysDictData);
    }


}
